package com.example.gokuplayalong.bases.network.updown;

import android.os.Handler;
import android.os.Message;

import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.threadpool.CustomThreadPoolExecutor;
import com.example.gokuplayalong.bases.tools.Logger;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Upload {

    private UploadDownInterface uploadDownInterface;
    private static Upload INSTANCE = null;

    public static Upload getInstance() {
        if (INSTANCE == null) INSTANCE = new Upload();
        return INSTANCE;
    }

    public void uploadFile(MediaType mediaType, String filename, String remoteFilename, Handler handler) {

        CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    File file = new File(AppParams.getInstance().mediaDir, filename);
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody fileBody = RequestBody.create(mediaType, file);
                    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                            .addFormDataPart("file", file.getName(), fileBody)
                            .addFormDataPart("filename", remoteFilename)
                            .build();
                    Request request = new Request.Builder().addHeader("Token", AppParams.getInstance().appCurrentUser.getJsonObject()
                                    .getString("token"))
                            .url(AppParams.getInstance().baseUrl + "/uploadFile")
                            .post(requestBody)
                            .build();


                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Message message = new Message();
                            message.what = 2;
                            handler.sendMessage(message);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        }
                    });
                } catch (Throwable e) {
                }
            }
        });
    }

    public void uploadImage(File orignalFile, String mimeType, String filename, UploadDownInterface uploadDownInterface) {
        CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    MediaType MEDIA_TYPE_PNG = MediaType.parse(mimeType);

                    RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, orignalFile);
                    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                            .addFormDataPart("file", orignalFile.getName(), fileBody)
                            .addFormDataPart("filename", filename)
                            .build();
                    Request request = new Request.Builder().url(AppParams.getInstance().uploadMediaLocationServerUrl)
                            .post(requestBody)
                            .addHeader("token", AppParams.getInstance().appCurrentUser.getJsonObject()
                                    .getString("token"))
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            uploadDownInterface.uploadFailed(e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            uploadDownInterface.uploadSuccess(response.body().string());
                        }
                    });
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

    }

    public void uploadImageByEncoded(File orignalFile, String mimeType, String userid, String filename, UploadDownInterface uploadDownInterface) {
        CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    MediaType MEDIA_TYPE_PNG = MediaType.parse(mimeType);

                    RequestBody fileBody = RequestBody.create(MEDIA_TYPE_PNG, orignalFile);
                    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                            .addFormDataPart("file", orignalFile.getName(), fileBody)
                            .addFormDataPart("userid", userid)
                            .addFormDataPart("filename", filename)
                            .build();
                    Request request = new Request.Builder().url(AppParams.getInstance().uploadMediaLocationServerUrl)
                            .post(requestBody)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            uploadDownInterface.uploadFailed(e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            uploadDownInterface.uploadSuccess(response.body().string());
                        }
                    });
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

    }

}
